home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / stevie.arc / PTRFUNC.C < prev    next >
Text File  |  1990-01-10  |  3KB  |  158 lines

  1. /*
  2.  * STevie - ST editor for VI enthusiasts.    ...Tim Thompson...twitch!tjt...
  3.  *
  4.  * Extensive modifications by:  Tony Andrews       onecom!wldrdg!tony
  5.  * Turbo C 1.5 port by: Denny Muscatelli 061988
  6.  */
  7.  
  8. #include "stevie.h"
  9.  
  10. /*
  11.  * The routines in this file attempt to imitate many of the operations
  12.  * that used to be performed on simple character pointers and are now
  13.  * performed on LPTR's. This makes it easier to modify other sections
  14.  * of the code. Think of an LPTR as representing a position in the file.
  15.  * Positions can be incremented, decremented, compared, etc. through
  16.  * the functions implemented here.
  17.  */
  18.  
  19. /*
  20.  * inc(p)
  21.  *
  22.  * Increment the line pointer 'p' crossing line boundaries as necessary.
  23.  * Return 1 when crossing a line, -1 when at end of file, 0 otherwise.
  24.  */
  25. int
  26. inc(lp)
  27. register LPTR    *lp;
  28. {
  29.     register char *p = &(lp->linep->s[lp->index]);
  30.  
  31.     if (*p != NUL) {            /* still within line */
  32.         lp->index++;
  33.         return ((p[1] != NUL) ? 0 : 1);
  34.     }
  35.  
  36.     if (lp->linep->next != Fileend->linep) {  /* there is a next line */
  37.         lp->index = 0;
  38.         lp->linep = lp->linep->next;
  39.         return 1;
  40.     }
  41.  
  42.     return -1;
  43. }
  44.  
  45. /*
  46.  * dec(p)
  47.  *
  48.  * Decrement the line pointer 'p' crossing line boundaries as necessary.
  49.  * Return 1 when crossing a line, -1 when at start of file, 0 otherwise.
  50.  */
  51. int
  52. dec(lp)
  53. register LPTR    *lp;
  54. {
  55.     if (lp->index > 0) {            /* still within line */
  56.         lp->index--;
  57.         return 0;
  58.     }
  59.  
  60.     if (lp->linep->prev != NULL) {        /* there is a prior line */
  61.         lp->linep = lp->linep->prev;
  62.         lp->index = strlen(lp->linep->s);
  63.         return 1;
  64.     }
  65.  
  66.     return -1;                /* at start of file */
  67. }
  68.  
  69. /*
  70.  * gchar(lp) - get the character at position "lp"
  71.  */
  72. int
  73. gchar(lp)
  74. register LPTR    *lp;
  75. {
  76.     return (lp->linep->s[lp->index]);
  77. }
  78.  
  79. /*
  80.  * pchar(lp, c) - put character 'c' at position 'lp'
  81.  */
  82. void
  83. pchar(lp, c)
  84. register LPTR    *lp;
  85. char    c;
  86. {
  87.     lp->linep->s[lp->index] = c;
  88. }
  89.  
  90. /*
  91.  * pswap(a, b) - swap two position pointers
  92.  */
  93. void
  94. pswap(a, b)
  95. register LPTR    *a, *b;
  96. {
  97.     LPTR    tmp;
  98.  
  99.     tmp = *a;
  100.     *a  = *b;
  101.     *b  = tmp;
  102. }
  103.  
  104. /*
  105.  * Position comparisons
  106.  */
  107.  
  108. bool_t
  109. lt(a, b)
  110. register LPTR    *a, *b;
  111. {
  112.     register int an, bn;
  113.  
  114.     an = LINEOF(a);
  115.     bn = LINEOF(b);
  116.  
  117.     if (an != bn)
  118.         return (an < bn);
  119.     else
  120.         return (a->index < b->index);
  121. }
  122.  
  123. bool_t
  124. gt(a, b)
  125. LPTR    *a, *b;
  126. {
  127.     register int an, bn;
  128.  
  129.     an = LINEOF(a);
  130.     bn = LINEOF(b);
  131.  
  132.     if (an != bn)
  133.         return (an > bn);
  134.     else
  135.         return (a->index > b->index);
  136. }
  137.  
  138. bool_t
  139. equal(a, b)
  140. register LPTR    *a, *b;
  141. {
  142.     return (a->linep == b->linep && a->index == b->index);
  143. }
  144.  
  145. bool_t
  146. ltoreq(a, b)
  147. register LPTR    *a, *b;
  148. {
  149.     return (lt(a, b) || equal(a, b));
  150. }
  151.  
  152. bool_t
  153. gtoreq(a, b)
  154. LPTR    *a, *b;
  155. {
  156.     return (gt(a, b) || equal(a, b));
  157. }
  158.